#include "LinkQueue.h"

void initQueue(LinkQueue& Q)
{
	Q.front = Q.rear = NULL;
}

void ClearQueue(LinkQueue& Q)
{
	LinkNode* p;
	while (Q.front)
	{
		p = Q.front;
		Q.front = p->link;
		free(p);
	}
	Q.rear = NULL;
}

bool QueueEmpty(LinkQueue& Q)
{
	return Q.front == NULL;
}

bool enQueue(LinkQueue& Q, QElemType x)
{
	LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
	s->data = x;
	s->link = NULL;
	if (Q.rear == NULL)
	{
		Q.front = Q.rear = s;
	}
	else
	{
		Q.rear->link = s;
		Q.rear = s;
	}
	return true;
}

bool deQueue(LinkQueue& Q, QElemType& x)
{
	if (Q.front == NULL)
	{
		return false;
	}
	LinkNode* p = Q.front;
	x = p->data;
	Q.front = p->link;
	free(p);
	if (Q.front == NULL)
	{
		Q.rear = NULL;
	}
	return true;
}

bool getFront(LinkQueue& Q, QElemType& x)
{
	if (Q.front == NULL)
	{
		return false;
	}
	x = Q.front->data;
	return true;
}

int QueueSize(LinkQueue& Q)
{
	LinkNode* p = Q.front;
	int k = 0;
	while (p)
	{
		p = p->link;
		k++;
	}
	return k;
}